package com.example.test;

import com.example.demo.pojo.Evection;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 *
 *
 * @author zhuyaotong
 * @date 2021/05/30
 */
public class ActivitiGatewayInclusive {

    private static final String processDefinitionKey = "inclusive";

    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .disableSchemaValidation().addClasspathResource("bpmn/evection-inclusive.bpmn")
                .name("出差申请流程-包含网关").deploy();

        System.out.println("流程部署id：" + deploy.getId());
        System.out.println("流程部署名字：" + deploy.getName());
    }

    /**
     * 启动流程的时候设置流程变量
     */
    @Test
    public void testStartProcess() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Evection evection = new Evection();
        evection.setNum(4d);

        Map<String, Object> map = new HashMap<>();
        map.put("evection", evection);

        // 启动流程
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, map);

        System.out.println("流程定义ID：" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例名称：" + processInstance.getName());
    }

    /**
     * 完成个人任务
     */
    @Test
    public void completeTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 获取任务Service
        TaskService taskService = processEngine.getTaskService();
//        String assignee = "tom";
//        String assignee = "miki";
        String assignee = "jack";

        // 查询任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(processDefinitionKey)
                .taskAssignee(assignee).singleResult();

        // 完成任务
        if (task != null) {
            taskService.complete(task.getId());
            System.out.println(task.getId() + "任务已完成");
        }
    }

}
